package com.matt.foundation.utils;

import com.matt.domain.Clientele;
import com.matt.domain.Leads;
import com.matt.domain.LeadsUser;
import com.matt.foundation.retention.FieldMeta;
import org.apache.commons.lang.StringUtils;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * *********************
 * description:比较两个对象修改后的数据
 * Created by Ron
 * from com.matt
 * on 2018/7/20 0020.
 * *********************
 */
public class ContrastStringFromOldAndNow<T> {


    /**
     * 比较修改前后实体 显示详细的修改信息
     * @param oldBean  修改前实体
     * @param newBean  修改后实体
     * @return
     */
    public String contrastObj(Object oldBean, Object newBean) {
        String str="";
        T pojo1 = (T) oldBean;
        T pojo2 = (T) newBean;
        try {
            Class clazz = pojo1.getClass();
            //获取T中属性的个数
            Field[] fields = pojo1.getClass().getDeclaredFields();
            int i=1;
            for (Field field : fields) {
                if("serialVersionUID".equals(field.getName())){
                    continue;
                }
                PropertyDescriptor pd = new PropertyDescriptor(field.getName(), clazz);
                Method getMethod = pd.getReadMethod();
                //获取属性的值
                Object o1 = getMethod.invoke(pojo1);
                Object o2 = getMethod.invoke(pojo2);

               String o1Str="";
               String o2Str="";
                if(o1!=null)
                    o1Str=o1.toString();
                if (o2!=null)
                    o2Str=o2.toString();

                if (!o1Str.equals(o2Str)) {
                    //获取注解的值
                    FieldMeta meta = field.getAnnotation(FieldMeta.class);
                    if(i!=1){
                        str+=";";
                    }
                    if (meta !=null){
                        String code = meta.name();
                        //1.线索
                        if (clazz== Leads.class) {
                            //特殊字段处理
                            if ("线索方式".equals(code) || "线索状态".equals(code) || "性别".equals(code) || "户籍类型".equals(code) ){
                            //switch (code) {
                                if ("线索方式".equals(code)){
                                    if (StringUtils.isBlank(o1Str)){
                                        str += " " + meta.name() +  " 修改为 \"" + leads_category(o2Str==""? null:new Integer(o2.toString())) + "\" "+'\n';
                                    }else
                                        str += " " + meta.name() + "\"" + leads_category(o1Str==""? null:new Integer(o1.toString())) + "\"  修改为 \"" + leads_category(o2Str==""? null:new Integer(o2.toString())) + "\" "+'\n';
                                }
                                if ("线索状态".equals(code))
                                    if (StringUtils.isBlank(o1Str)){
                                        str += " " + meta.name() + "  修改为 \"" + leads_status(o2Str==""? null:new Integer(o2.toString())) + "\" "+'\n';
                                    }else{
                                        str += " " + meta.name() + "\"" + leads_status(o1Str==""? null:new Integer(o1.toString())) + "\"  修改为 \"" + leads_status(o2Str==""? null:new Integer(o2.toString())) + "\" "+'\n';
                                    }

                                if ("性别".equals(code))
                                    if (StringUtils.isBlank(o1Str)){
                                        str += " " + meta.name() + "  修改为 \"" + gender(o2Str==""? null:new Integer(o2.toString())) + "\" "+'\n';
                                    }else
                                        str += " " + meta.name() + "\"" + gender(o1Str==""? null:new Integer(o1.toString())) + "\"  修改为 \"" + gender(o2Str==""? null:new Integer(o2.toString())) + "\" "+'\n';
                                if ("户籍类型".equals(code))
                                    if (StringUtils.isBlank(o1Str)){
                                        str += " " + meta.name() + "  修改为 \"" + leads_registerType(o2Str==""? "":o2.toString()) + "\" "+'\n';
                                    }else{
                                        str += " " + meta.name() + "\"" + leads_registerType(o1Str==""? "":o1.toString()) + "\"  修改为 \"" + leads_registerType(o2Str==""? "":o2.toString()) + "\" "+'\n';
                                    }
                               // }
                             }else{
                                if(o1==null || StringUtils.isBlank(o1.toString()))
                                    str+= " "+meta.name()+" 修改为 \""+object_string(o2)+ "\" "+'\n';
                                else
                                    str+= " "+meta.name()+"\""+object_string(o1) +"\"  修改为 \""+object_string(o2)+ "\" "+'\n';
                              }
                        }
                        //2.客户
                        if (clazz== Clientele.class){
                            //特殊字段处理
                            if ("客户状态".equals(code) || "性别".equals(code) || "客户分类".equals(code) || "户籍类型".equals(code) ){
                                //switch (code) {
                                if ("客户状态".equals(code))
                                    if (StringUtils.isBlank(o1Str)){
                                        str += " " + meta.name() + "  修改为 \"" + clientele_status(o2Str==""? null:new Integer(o2.toString())) + "\" "+'\n';
                                    }else
                                        str += " " + meta.name() + "\"" + clientele_status(o1Str==""? null:new Integer(o1.toString())) + "\"  修改为 \"" + clientele_status(o2Str==""? null:new Integer(o2.toString())) + "\" "+'\n';

                                if ("性别".equals(code))
                                    if (StringUtils.isBlank(o1Str)){
                                        str += " " + meta.name() + "  修改为 \"" + gender(o2Str==""? null:new Integer(o2.toString())) + "\" "+'\n';
                                    }else
                                        str += " " + meta.name() + "\"" + gender(o1Str==""? null:new Integer(o1.toString())) + "\"  修改为 \"" + gender(o2Str==""? null:new Integer(o2.toString())) + "\" "+'\n';
                                if ("客户分类".equals(code))
                                    if (StringUtils.isBlank(o1Str)){
                                        str += " " + meta.name() + "  修改为 \"" + clientele_clienteleType(o2Str==""? null:o2.toString()) + "\" "+'\n';
                                    }else
                                        str += " " + meta.name() + "\"" + clientele_clienteleType(o1Str==""? null:o1.toString()) + "\"  修改为 \"" + clientele_clienteleType(o2Str==""? null:o2.toString()) + "\" "+'\n';
                                if ("户籍类型".equals(code))
                                    if (StringUtils.isBlank(o1Str)){
                                        str += " " + meta.name() + "  修改为 \"" + leads_registerType(o2Str==""? "":o2.toString()) + "\" "+'\n';
                                    }else
                                        str += " " + meta.name() + "\"" + leads_registerType(o1Str==""? "":o1.toString()) + "\"  修改为 \"" + leads_registerType(o2Str==""? "":o2.toString()) + "\" "+'\n';

                            }else{
                                if (o1== null || StringUtils.isBlank(o1.toString()))
                                    str+= " "+meta.name()+"  修改为 \""+object_string(o2) + "\" "+'\n';
                                else
                                    str+= " "+meta.name()+"\""+object_string(o1) +"\"  修改为 \""+object_string(o2) + "\" "+'\n';
                            }
                        }
                        //3.联系人
                        if (clazz== LeadsUser.class){
                            if (o1== null || StringUtils.isBlank(o1.toString()))
                                str+= " "+meta.name()+"  修改为 \""+object_string(o2) + "\" "+'\n';
                            else
                                str+= " "+meta.name()+"\""+object_string(o1) +"\"  修改为 \""+object_string(o2) + "\" "+'\n';
                        }

                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;
    }

    /**
     * Object转为字符串
     * @param ob
     * @return
     */
    public static String object_string(Object ob){
        if (ob==null)
            return "";
        return ob.toString();
    }


    /**
     * 转义线索方式
     * @param category
     * @return
     */
    public static String leads_category(Integer category){

        if(category!=null){
            switch (category){
                case 1:
                    return "主动";
                case 2:
                    return "被动";
            }
        }
       return "";
    }

    /**
     * 转义线索状态
     * @param status
     * @return
     */
    public static String leads_status(Integer status){

        if(status!=null){
            switch (status){
                case 1:
                    return "未处理";
                case 2:
                    return "初访";
                case 3:
                    return "已邀约";
                case 4:
                    return "已到访";
                case 5:
                    return "不敢兴起";
                case 6:
                    return "未接通";
                case 7:
                    return "地域不符";
                case 8:
                    return "孩子超龄";
                case 9:
                    return "无效的联系方式";
            }
        }
        return "";
    }

    /**
     * 转义性别
     * @param gender
     * @return
     */
    public static String gender(Integer gender){

        if(gender!=null){
            switch (gender){
                case 1:
                    return "男";
                case 0:
                    return "女";
            }
        }
        return "";
    }

    /**
     * 转义户籍类型
     * @param registerType
     * @return
     */
    public static String leads_registerType(String  registerType){

        if(StringUtils.isNotBlank(registerType)){
            switch (registerType){
                case "1":
                    return "本片";
                case "2":
                    return "本区外片";
                case "3":
                    return "本市外区";
                case "4":
                    return "外籍";
                case "5":
                    return "港澳台";
            }
        }
        return "";
    }

    /**
     * 转义客户状态
     * @param status
     * @return
     */
    public static String clientele_status(Integer status){

        if(status!=null){
            switch (status){
                case 1:
                    return "未处理";
                case 2:
                    return "已预约";
                case 3:
                    return "已到访";
                case 4:
                    return "已完成申请表";
                case 5:
                    return "已面试";
                case 6:
                    return "已交定金";
                case 7:
                    return "面试不通过";
                case 8:
                    return "方案不满意";
                case 9:
                    return "园区不满意";
                case 10:
                    return "无支付支付能力";
            }
        }
        return "";
    }

    /**
     * 转义客户分类
     * @param clienteleType
     * @return
     */
    public static String clientele_clienteleType(String clienteleType){
        if(StringUtils.isNotBlank(clienteleType)){
            switch (clienteleType){
                case "A":
                    return "有支付能力也有消费意愿";
                case "B":
                    return "有支付能力消费意愿一般";
                case "C":
                    return "有能力支付受限消费意愿强烈";
                case "D":
                    return "无支付能力";
            }
        }
        return "";
    }


}
